
#include <pspkernel.h>
#include <psputility.h>
#include <psputility_htmlviewer.h>
#include <stdlib.h>
#include <string.h>
#include "pspvideo.h"

static pspUtilityHtmlViewerParam html;

int setBrowserParam(SceUID vpl, unsigned int vpl_size, void* data, unsigned int size)
{
    int ret;

	vpl = sceKernelCreateVpl("BrowserVpl", PSP_MEMORY_PARTITION_USER, 0, vpl_size, NULL);
    if (vpl < 0)
    {
        return vpl;
    }
    if ((ret = sceKernelAllocateVpl(vpl, size, &data, NULL)) < 0)
	{
	    sceKernelDeleteVpl(vpl);
		return ret;
	}
    memset(&html, 0, sizeof(html));
    html.base.size = sizeof(html);
    sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_LANGUAGE, &html.base.language);
    sceUtilityGetSystemParamInt(PSP_SYSTEMPARAM_ID_INT_UNKNOWN, &html.base.buttonSwap);
    html.base.graphicsThread = 0x11;
    html.base.accessThread = 0x13;
    html.base.fontThread = 0x12;
    html.base.soundThread = 0x10;
    html.memaddr = data;
	html.memsize = size;
	html.unknown1 = 0;
	html.unknown2 = 0;
	html.initialurl = NULL;
	html.numtabs = 1;
	html.interfacemode = PSP_UTILITY_HTMLVIEWER_INTERFACEMODE_FULL;
	html.options = PSP_UTILITY_HTMLVIEWER_DISABLE_STARTUP_LIMITS
                 | PSP_UTILITY_HTMLVIEWER_DISABLE_EXIT_DIALOG
                 | PSP_UTILITY_HTMLVIEWER_DISABLE_TAB_DISPLAY;
	/* WITHOUT 'ms0:' on the paths */
	html.dldirname = NULL;
    html.dlfilename = NULL;
    html.uldirname = NULL;
    html.ulfilename = NULL;
	html.cookiemode = PSP_UTILITY_HTMLVIEWER_COOKIEMODE_DEFAULT;
	html.unknown3 = 0;
	html.homeurl = NULL;
	html.textsize = PSP_UTILITY_HTMLVIEWER_TEXTSIZE_NORMAL;
	html.displaymode = PSP_UTILITY_HTMLVIEWER_DISPLAYMODE_SMART_FIT;
	html.connectmode = PSP_UTILITY_HTMLVIEWER_CONNECTMODE_MANUAL_ALL;
	html.disconnectmode = PSP_UTILITY_HTMLVIEWER_DISCONNECTMODE_ENABLE;
    return 0;
}

int browser(char* url)
{
    int done=0, ret;

	html.initialurl = url;
	html.homeurl = url;
    if ((ret = sceUtilityHtmlViewerInitStart(&html)) < 0)
	{
		return ret;
	}
    while(!done)
    {
        minimalRender();
        switch(sceUtilityHtmlViewerGetStatus())
        {
            case PSP_UTILITY_DIALOG_NONE:
                break;
            case PSP_UTILITY_DIALOG_INIT :
                break;
            case PSP_UTILITY_DIALOG_VISIBLE:
                sceUtilityHtmlViewerUpdate(1);
                break;
            case PSP_UTILITY_DIALOG_QUIT:
                sceUtilityHtmlViewerShutdownStart();
                break;
            case PSP_UTILITY_DIALOG_FINISHED :
                done = 1;
                break;
        }
        sceKernelDelayThread(5*1000);
        videoFlipScreen(1);
    }
    return 0;
}
